/**
 * 语言storestore
 */
import { makeAutoObservable } from "mobx";

import { fetchAdd, fetchAll, fetchUpdate, fetchDelete } from "../api/language";

import { ITResponseData, checkResonse } from "../api";

// 语言，目前只支持英文和中文
declare const LangTypes: ["en", "zhcn"];
declare type LangType = typeof LangTypes[number];

export interface ITLangItem {
  id: string;
  key: string;
  en: string;
  cn: string;
  desc: string;
}

interface ITBadResp {
  msg: string;
}

interface ITObj {
  [propName: string]: string;
}

export interface ITLanguageStore {
  langCur: LangType;
  changeLangCur(cur: any): void;

  // 语言数据
  lang: ITObj;

  changeLang: () => void;

  // 替换语言中的变量
  replaceLang: (langstr: string, reptext: string) => string;

  // 是否显示新增，编辑弹窗
  showAddEditModal: boolean;

  // 修改是否显示新增，编辑弹窗
  changeShowAddEditModal: (data: boolean) => void;

  // 当前正在修改的语言数据
  editLanguageData: any;

  // 修改当前正在修改的语言数据
  changeEditLanguageData: (data: any) => void;

  // 新增语言loading
  showAddLoading: boolean;

  // 修改新增语言loading
  changeShowAddLoading: (data: boolean) => void;

  // 新增语言
  addLanguage: (data: any) => Promise<ITBadResp | null>;

  // 查询所有loading
  allLoading: boolean;

  // 修改查询所有loading
  changeAllLoading: (data: boolean) => void;

  // 搜索的语言结果
  searchLanguage: ITLangItem[];

  // 修改搜索的语言结果
  changeSearchLanguage: (data: ITLangItem[]) => void;

  // 所有语言
  allLanguage: ITLangItem[];

  // 修改所有语言
  changeAllLanguage: (data: ITLangItem[]) => void;

  //查询所有语言
  getAllLanguage: (isModify: boolean) => void;

  // 修改语言请求状态
  editLoading: boolean;
  changeEditLoading: (data: boolean) => void;

  // 修改语言
  updateLanguage: (data: any) => Promise<ITBadResp | null>;

  // 删除语言请求状态
  delLoading: boolean;

  changeDelLoading: (data: boolean) => void;

  // 删除语言
  deleteLanguage: (data: any) => Promise<boolean>;
}

class LanguageStore {
  constructor() {
    makeAutoObservable(this, {}, { autoBind: true });
  }

  langCur: LangType = "en";
  changeLangCur(data: any) {
    this.langCur = data;
    localStorage.setItem("lang", data);
  }

  // 替换语言中的变量
  replaceLang = (langstr: string, reptext: string): string => {
    return langstr.replace(/\{1\}/, reptext);
  };

  lang: ITObj = {};
  changeLang = () => {
    let list: ITObj = {};
    this.allLanguage.forEach(({ key, en, cn }: ITLangItem) => {
      let val = en;
      if (this.langCur === "zhcn") {
        val = cn;
      }
      list[key] = val;
    });
    this.lang = list;
  };

  showAddEditModal = false;

  changeShowAddEditModal = (data: boolean) => (this.showAddEditModal = data);

  editLanguageData = null;

  changeEditLanguageData = (data: any) => (this.editLanguageData = data);

  /**
   * 新增
   */
  showAddLoading = false;
  changeShowAddLoading = (data: boolean) => (this.showAddLoading = data);

  // 新增语言
  addLanguage = async (data: any): Promise<ITBadResp | any> => {
    this.changeShowAddLoading(true);
    const res = await fetchAdd(data);
    this.changeShowAddLoading(false);
    if (checkResonse(res)) {
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };

  /**
   * 查询所有
   */
  allLoading = false;
  changeAllLoading = (data: boolean) => (this.allLoading = data);

  allLanguage: ITLangItem[] = [];
  changeAllLanguage = (data: ITLangItem[]) => (this.allLanguage = data);

  // 搜索的语言结果
  searchLanguage: ITLangItem[] = [];

  changeSearchLanguage = (data: ITLangItem[]) => (this.searchLanguage = data);

  // 请求所有语言
  getAllLanguage = async (isModify: boolean) => {
    const sesLang: string | null = localStorage.getItem("language");
    if (!!sesLang && !isModify) {
      try {
        const langJson = JSON.parse(sesLang);
        this.changeAllLanguage(langJson);
        this.changeLang();
      } catch (e) {}
    } else {
      this.changeAllLoading(true);
      const res: any = await fetchAll();
      if (checkResonse(res)) {
        const result: ITResponseData = res.data;
        const { data } = result;
        this.changeAllLanguage(data);
        this.changeLang();
        localStorage.setItem("language", JSON.stringify(data));
      }
      this.changeAllLoading(false);
    }
  };

  /**
   * 修改语言
   */
  editLoading = false;
  changeEditLoading = (data: boolean) => (this.editLoading = data);
  updateLanguage = async (data: any): Promise<ITBadResp | null> => {
    this.changeEditLoading(true);
    const res = await fetchUpdate(data);
    this.changeEditLoading(false);
    if (checkResonse(res)) {
      return null;
    }
    const { message } = res.data;
    return {
      msg: message,
    };
  };

  /**
   * 删除语言
   */
  delLoading = false;
  changeDelLoading = (data: boolean) => (this.delLoading = data);
  deleteLanguage = async (data: any): Promise<boolean> => {
    this.changeDelLoading(true);
    const res = await fetchDelete(data);
    this.changeDelLoading(false);
    if (checkResonse(res)) {
      return true;
    }
    return false;
  };
}

const languageStore: ITLanguageStore = new LanguageStore();
export default languageStore;
